<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 15/9/25
 * Time: 下午5:56
 */
class WxwProductController extends BaseController {
    /*
     * test
     * */
    public function test(){
        $validator = Validator::make(Input::all(),
            array(
                'phone' => 'required',
//                'area' => 'required',
                'check_code' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_login_args_error_1011");
                break;
            }

            //传入参数
            $phone = Input::get('phone');

        } while (false);
        Log::info("info", array("system" => "wxw" ,"event" => "test", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 产品项信息
     * */
    public function base_package_item(){
        $validator = Validator::make(Input::all(),
            array(
//                'token' => 'required',
//                'city' => 'required',
//                'place' => 'required',
                'product_id' => 'required',
                'package_id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array('count' => 0,'products' => array()));
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_login_args_error_1011");
                break;
            }

            //传入参数
//            $token = Input::get('token');
            $type = 'base';
//            $city = Input::get('city');
//            $place = Input::get('place');
            $product_id = Input::get('product_id');
            $package_id = Input::get('package_id');
            $callback = Input::get('callback');

            //验证token
//            $user_data = Users::getUserByToken($token);
//            if(is_numeric($user_data) && $user_data < 0){
//                $response = Lang::get("error.user_token_not_exist_1004");
//                break;
//            }

            //获取基础产品－产品包－产品项信息
            //基础产品
//            $product = Product::where('id',$product_id)
//                ->where('type',$type)
//                ->where('status',1)
//                ->first();
            $package = ProductPackage::where('id',$package_id)
                ->where('status',1)
                ->first();
            $items = array();
            $item_data = ProductPackItem::where('pack_id',$package_id)
                ->where('status',1)
                ->orderBy('type', 'asc')
                ->get();
            foreach($item_data as $_item){
                if($_item['type'] == 1){ //耗材
                    $item = SupplySeries::where('id',$_item['item_id'])
                        ->where('status',1)
                        ->select(array('id as item_id','name as item_name','amount'))
                        ->get()->toArray();
                }elseif($_item['type'] == 2){ //服务
                    $item = ServiceProductItem::where('id',$_item['item_id'])
                        ->where('status',1)
                        ->select(array('id as item_id','name as item_name','time as amount'))
                        ->get()->toArray();
                }
                $item[0]['num'] = 1;
                array_push($items,$item[0]);
            }

//            $result = array(
//                'product_id' => $product_id,
//                'product_name' => $product->name,
//                'packages' => array(
//                    array(
//                        'package_id' => $package_id,
//                        'package_name' => $package->name,
//                        'items' => $items
//                    ),
//                ),
//            );
            $result = array(
                'package_id' => $package_id,
                'package_name' => $package->name,
                'item' => $items
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);

        if(is_null($callback)){
            $response = json_encode($response);
        }else{
            $response = $callback . "('" . json_encode($response) . "')";

        }
        Log::info("info", array("system" => "wxw" ,"event" => "base_package", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }

    /*
     * 基础产品包信息
     * */
    public function base_package(){
        $validator = Validator::make(Input::all(),
            array(
//                'token' => 'required',
//                'city' => 'required',
//                'place' => 'required',
                'product_id' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array('count' => 0,'products' => array()));
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_login_args_error_1011");
                break;
            }

            //传入参数
//            $token = Input::get('token');
            $type = 'base';
            $city = Input::get('city');
            $place = Input::get('place');
            $product_id = Input::get('product_id');
            $result = array(
                'count' => 0,
                'products' => array()
            );
            $callback = Input::get('callback');

            //验证token
//            $user_data = Users::getUserByToken($token);
//            if(is_numeric($user_data) && $user_data < 0){
//                $response = Lang::get("error.user_token_not_exist_1004");
//                break;
//            }

            //获取产品包ID
            $packages = Product::where('city',$city)
                ->where('status',1)
                ->where('type',$type)
                ->where('place',$place)
                ->where('id',$product_id)
                ->select(array('package_id'))
                ->get()->toArray();
            if(count($packages) > 0){
                if(strlen($packages[0]['package_id']) > 0){
                    $package_id = explode(",",$packages[0]['package_id']);
                    $packages = ProductPackage::leftJoin('product_package_price', 'product_package.id', '=', 'product_package_price.product_package_id')
                        ->whereIn('product_package.id',$package_id)
                        ->where('product_package.status',1)
                        ->where('product_package.place',$place)
                        ->select(array('product_package.id','product_package.name','product_package.desc','product_package.icon','product_package_price.amount'))
                        ->orderBy('product_package.sort', 'asc')
                        ->get()->toArray();

                    $count = count($packages);
                    $result = array(
                        'count' => $count,
                        'products' => $packages
                    );
                }
            }
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);

        if(is_null($callback)){
            $response = json_encode($response);
        }else{
            $response = $callback . "('" . json_encode($response) . "')";

        }
        Log::info("info", array("system" => "wxw" ,"event" => "base_package", 'input' =>Input::all() ,"output" => $response));
        return $response;
    }

    /*
     * 基础产品信息接口
     * */
    public function base(){
        $validator = Validator::make(Input::all(),
            array(
//                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_login_args_error_1011");
                break;
            }

            //传入参数
//            $token = Input::get('token');
            $type = 'base';
            $city = Input::get('city')?Input::get('city'):1;
            $place = Input::get('place')?Input::get('place'):1;

            //验证token
//            $user_data = Users::getUserByToken($token);
//            if(is_numeric($user_data) && $user_data < 0){
//                $response = Lang::get("error.user_token_not_exist_1004");
//                break;
//            }

            //获取产品信息
            $products = Product::where('city',$city)
                ->where('status',1)
                ->where('type',$type)
                ->where('place',$place)
                ->select(array('id','name','desc','icon'))
                ->orderBy('sort', 'asc')
                ->get()->toArray();

            $count = count($products);
            $result = array(
                'count' => $count,
                'products' => $products
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "wxw" ,"event" => "base", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }

    /*
     * 推广产品信息接口
     * */
    public function promotion(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );

        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("error.user_login_args_error_1011");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $type = 'promotion';
            $city = Input::get('city')?Input::get('city'):1;
            $now = Common::num2DateTime(time());

            //验证token
            $user_data = Users::getUserByToken($token);
            if(is_numeric($user_data) && $user_data < 0){
                $response = Lang::get("error.user_token_not_exist_1004");
                break;
            }

            //获取产品信息
            $products = Product::leftJoin('price', 'product.id', '=', 'price.product_id')
                ->where('product.city',$city)
                ->where('product.status',1)
                ->where('product.type',$type)
                ->where('product.start_date','<',$now)
                ->where('product.stop_date','>',$now)
                ->select(array('product.id','product.name','product.desc','product.icon','product.url','product.place','price.amount'))
                ->orderBy('product.sort', 'asc')
                ->get()->toArray();
            $count = count($products);
            $result = array(
                'count' => $count,
                'products' => $products
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "wxw" ,"event" => "promotion", 'input' =>Input::all() ,"output" => $response));
        return json_encode($response);
    }
}